
#include <BScript>
#include <BPainter>
#include <BApplication>

#include "StatusBoard.h"

StatusBoard::StatusBoard(BWidget* parent) : BGroupBox(parent)
{
	_counter = 0;
}
StatusBoard::StatusBoard(const BString& title, BWidget* parent) : BGroupBox(title, parent)
{
	_counter = 0;
}
StatusBoard::~StatusBoard()
{

}

bool StatusBoard::setAsset(const BValue& asset)
{
	if (this->asset() != asset)
	{
		if (BScript* script = this->asset())
		{
			disconnect(script);
		}
		BGroupBox::setAsset(asset);
		if (BScript* script = this->asset())
		{
			connect(script, Signal_Performed, &StatusBoard::slotScriptPerformed);
		}
	}
	return false;
}

void StatusBoard::setThread(BThread* thread)
{
	_thread = thread;
}
BThread* StatusBoard::thread() const
{
	return _thread;
}

void StatusBoard::slotScriptPerformed(BObject* object, const BValue& value)
{
	_counter = value;
	this->fresh();
}

void StatusBoard::updateEvent(const BEvent& event)
{
	this->fresh();
}

void StatusBoard::freshEvent(const BEvent& event)
{
	BGroupBox::freshEvent(event);
}
void StatusBoard::paintEvent(const BEvent& event)
{
	BGroupBox::paintEvent(event);
	const BRect& crect = this->centerRect();
	BPainter painter(this);
	painter.setColor(Color_Text);
	painter.drawText(crect, BString(_counter), Align_BottomCenter);

	painter.setColor(Color_Text);
	painter.drawText(crect, BString(BApplication::realFrequency()), Align_TopCenter);
}
